package net.flowas.genericcrud.service.impl;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import net.flowas.genericcrud.ExecuteContext;
import net.flowas.genericcrud.spi.ProcessListener;

public class BpmnFlowkeyFinderProcessListener implements ProcessListener {

  @Override
  public void process(ExecuteContext req) {
	Object entity = req.getQueryOption().getExample();
    String data = null;//entity.getString("data");
    InputStream is = new ByteArrayInputStream(data.getBytes());
    try {
      Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
      Element process = (Element) doc.getDocumentElement().getElementsByTagName("process").item(0);
      String flowKey = process.getAttribute("id");
      //Caused by: java.lang.UnsupportedOperationException
      //     at java.util.AbstractMap.put(AbstractMap.java:209) ~[?:?]
      //entity.put("name", Json.createValue(flowKey));
    } catch (SAXException | IOException | ParserConfigurationException e) {
      throw new IllegalArgumentException("not a valid xml!");
    }
  }
}
